package com.vvlive.caibao.smartcity.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;

import com.vvlive.caibao.smartcity.utils.ScreenUtil;

import smartcity.vvlive.caibao.com.smartc.R;

/**
 * Created by zhj on 2015/11/2.
 * 滑动进行编辑和删除
 */
public class SlideCtrlView extends HorizontalScrollView implements View.OnTouchListener {

    private static final int DEFAULT_INFO_LAYOUT = R.layout.item_personal_meeting_list;
    private static final String TAG = "SlideCtrlView";
    private static final int DEFAULT_BUTTON_WIDTH_DIP = 80;

    private int mInfoLayoutRes;
    private ScreenUtil mScreen;
    private LinearLayout mWrapper;
    private LinearLayout mInfoLayout;
    private LinearLayout mButtonWrapper;

    private int mButtonWidth;
    private boolean mIsCtrlShown = false;
    private Object data;

    private boolean mHasMoved = false;

    private CtrlClickListener mCtrlClick;
    private OnCtrlClickListener onCtrlClickListener;
    private OnInfoClickListener onInfoClickListener;
    private OnActionDownListener onActionDownListener;

    public SlideCtrlView(Context context) {
        this(context, null);
    }

    public SlideCtrlView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mScreen = new ScreenUtil(context);
        mButtonWidth = (int) this.mScreen.dip2px(DEFAULT_BUTTON_WIDTH_DIP);

        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.SlideCtrlView);
        mInfoLayoutRes = ta.getResourceId(R.styleable.SlideCtrlView_infoLayout, DEFAULT_INFO_LAYOUT);
        ta.recycle();

        this.mWrapper = (LinearLayout) inflate(context, R.layout.item_meeting_slide_to_ctrl, null);
        this.mInfoLayout = (LinearLayout) this.mWrapper.findViewById(R.id.ctrl_info);
        this.mButtonWrapper = (LinearLayout) this.mWrapper.findViewById(R.id.ctrl_buttons);
        this.mInfoLayout.setLayoutParams(new LinearLayout.LayoutParams(mScreen.getWidth(),
                                                                              LinearLayout.LayoutParams.MATCH_PARENT));
        this.addView(mWrapper);
        this.mCtrlClick = new CtrlClickListener();
        this.mButtonWrapper.getChildAt(0).setOnClickListener(this.mCtrlClick);
        this.mButtonWrapper.getChildAt(1).setOnClickListener(this.mCtrlClick);
        this.setOnTouchListener(this);
        this.setHorizontalScrollBarEnabled(false);
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN:
                mHasMoved = false;
                if (onActionDownListener != null) {
                    onActionDownListener.onActionDown();
                }
                break;
            case MotionEvent.ACTION_MOVE:
                mHasMoved = true;
                break;
            case MotionEvent.ACTION_UP:
                if (mIsCtrlShown) {
                    this.smoothScrollTo(0, 0);
                    mIsCtrlShown = false;
                } else {
                    if (this.getScrollX() > mButtonWidth/2) {
                        this.smoothScrollTo(mButtonWidth * 2, 0);
                        mIsCtrlShown = true;
                    } else {
                        this.smoothScrollTo(0, 0);
                        mIsCtrlShown = false;
                    }
                }
                if (!mHasMoved) {
                    if (onInfoClickListener != null) {
                        onInfoClickListener.onInfoClick(this);
                    }
                }
                return true;
        }
        return false;
    }

    class CtrlClickListener implements OnClickListener {

        @Override
        public void onClick(View v) {
            int id = v.getId();
            switch (id) {
                case R.id.ctrl_edit:
                    if (onCtrlClickListener != null) {
                        onCtrlClickListener.onEdit(SlideCtrlView.this);
                    }
                    break;

                case R.id.ctrl_del:
                    if (onCtrlClickListener != null) {
                        onCtrlClickListener.onDelete(SlideCtrlView.this);
                    }
                    break;
            }
        }
    }

    public interface OnCtrlClickListener {

        void onEdit(SlideCtrlView v);

        void onDelete(SlideCtrlView v);
    }

    public void setOnCtrlClickListener(OnCtrlClickListener onCtrlClickListener) {
        this.onCtrlClickListener = onCtrlClickListener;
    }

    public interface OnInfoClickListener {
        void onInfoClick(SlideCtrlView view);
    }

    public void setOnInfoClickListener(OnInfoClickListener onInfoClickListener) {
        this.onInfoClickListener = onInfoClickListener;
    }

    public interface OnActionDownListener {
        void onActionDown();
    }

    public void setOnActionDownListener(OnActionDownListener onActionDownListener) {
        this.onActionDownListener = onActionDownListener;
    }

    public boolean isCtrlShown() {
        return mIsCtrlShown;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }
}
